import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/fonts.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/model/product.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';

///
/// 已下架
///
class Delist extends StatefulWidget {
  const Delist({Key? key}) : super(key: key);

  @override
  State<Delist> createState() => _DelistState();
}

class _DelistState extends State<Delist> {
  List<ProductModel> products = [];

  bool isAll = false;

  @override
  void initState() {
    super.initState();
    products.add(ProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i1/2939922051/O1CN01kPFaY01R1P6vrXVlN_!!2939922051.jpg_Q75.jpg_.webp',
        name: 'A101 河村石磨肠粉(双蛋双蛋)',
        inventory: 50,
        actualPrice: 100,
        salePrice: 120,
        mark: ProductMarkEnum.expire));
    products.add(ProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i1/2939922051/O1CN01kPFaY01R1P6vrXVlN_!!2939922051.jpg_Q75.jpg_.webp',
        name: 'A101 河村石磨肠粉(双蛋双蛋)',
        inventory: 50,
        actualPrice: 100,
        salePrice: 120,
        mark: ProductMarkEnum.overtTime));
    products.add(ProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i1/2939922051/O1CN01kPFaY01R1P6vrXVlN_!!2939922051.jpg_Q75.jpg_.webp',
        name: 'A101 河村石磨肠粉(双蛋双蛋)',
        inventory: 50,
        actualPrice: 100,
        mark: ProductMarkEnum.soldOut));
    products.add(ProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i1/2939922051/O1CN01kPFaY01R1P6vrXVlN_!!2939922051.jpg_Q75.jpg_.webp',
        name: 'A101 河村石磨肠粉(双蛋双蛋)',
        inventory: 50,
        actualPrice: 100,
        mark: ProductMarkEnum.review));
    products.add(ProductModel(
        imgUrl:
            'https://gw.alicdn.com/imgextra/i1/2939922051/O1CN01kPFaY01R1P6vrXVlN_!!2939922051.jpg_Q75.jpg_.webp',
        name: 'A101 河村石磨肠粉(双蛋双蛋)',
        inventory: 50,
        actualPrice: 100,
        mark: ProductMarkEnum.auditFailure));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(color: Colors.white),
      padding: const EdgeInsets.symmetric(horizontal: Sizes.default15),
      child: Column(
        children: [
          Container(
            height: Sizes.default40,
            alignment: Alignment.centerLeft,
            child: InkWell(
                onTap: () {
                  setState(() {
                    isAll = !isAll;
                    if (isAll) {
                      for (var p in products) {
                        p.isSelected = isAll;
                      }
                    }
                  });
                },
                child: BrnCheckbox(
                  radioIndex: 0,
                  onValueChangedAtIndex: (int index, bool value) {
                    setState(() {
                      isAll = value;
                      for (var p in products) {
                        p.isSelected = isAll;
                      }
                    });
                  },
                  isSelected: isAll,
                  childOnRight: true,
                  child: Text('全选',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color515, fontSize: Sizes.default15)),
                )),
          ),
          Expanded(
              child: ListView.builder(
            itemBuilder: (BuildContext ctx, int index) {
              var product = products[index];
              return BrnCheckbox(
                childOnRight: true,
                radioIndex: index,
                isSelected: product.isSelected,
                iconPadding: const EdgeInsets.only(right: Sizes.default15),
                onValueChangedAtIndex: (int idx, bool value) {
                  setState(() {
                    product.isSelected = value;
                  });
                },
                child: Expanded(
                  child: Column(
                    children: [
                      Container(
                        padding:
                            const EdgeInsets.only(top: Sizes.default10, bottom: Sizes.default10),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(
                              width: 70,
                              child: Stack(
                                children: [
                                  GFImageOverlay(
                                      width: Sizes.default70,
                                      height: Sizes.default70,
                                      image: NetworkImage(product.imgUrl),
                                      borderRadius: BorderRadius.circular(Sizes.default5)),
                                  if (product.mark != null)
                                    Positioned(
                                        height: 16,
                                        bottom: 0,
                                        left: 0,
                                        right: 0,
                                        child: Container(
                                          alignment: Alignment.center,
                                          decoration: BoxDecoration(
                                              color: ColorsUtils.parseColorFromHexString(
                                                  '${product.mark?.backgroundColor}'),
                                              borderRadius: const BorderRadius.only(
                                                  bottomLeft: Radius.circular(Sizes.default5),
                                                  bottomRight: Radius.circular(Sizes.default5))),
                                          child: Text(
                                            '${product.mark?.text}',
                                            style: StylesUtils.customTextStyle(
                                                fontSize: Sizes.default12,
                                                color: '${product.mark?.textColor}'),
                                          ),
                                        )),
                                  if (product.classify != null)
                                    Positioned(
                                        width: 30,
                                        height: 15,
                                        top: 0,
                                        left: 0,
                                        child: Container(
                                          constraints:
                                              const BoxConstraints.expand(width: 30, height: 15),
                                          alignment: Alignment.center,
                                          decoration: BoxDecoration(
                                              color: ColorsUtils.parseColorFromHexString(
                                                  '${product.classify?.backgroundColor}'),
                                              borderRadius: const BorderRadius.only(
                                                  topLeft: Radius.circular(Sizes.default5),
                                                  bottomRight: Radius.circular(Sizes.default5))),
                                          child: Text(
                                            '${product.classify?.text}',
                                            style: StylesUtils.customTextStyle(
                                                color: '${product.classify?.textColor}',
                                                fontSize: Sizes.default12),
                                          ),
                                        ))
                                ],
                              ),
                            ),
                            Expanded(
                                child: Container(
                              padding: const EdgeInsets.only(left: Sizes.default10),
                              child: Column(
                                children: [
                                  Container(
                                    alignment: Alignment.centerLeft,
                                    margin: const EdgeInsets.only(bottom: Sizes.default5),
                                    child: Text(
                                      product.name,
                                      style:
                                          StylesUtils.customTextStyle(color: ColorsUtils.color333),
                                      overflow: TextOverflow.ellipsis,
                                      maxLines: 1,
                                    ),
                                  ),
                                  Container(
                                      alignment: Alignment.centerLeft,
                                      margin: const EdgeInsets.only(bottom: Sizes.default5),
                                      child: Text.rich(TextSpan(
                                        children: [
                                          TextSpan(
                                            text: '库存 ${product.inventory}',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color999,
                                                fontSize: Sizes.default12),
                                          ),
                                          WidgetSpan(
                                              child: Icon(
                                                AntdIcons.edit,
                                                color: ColorsUtils.parseColorFromHexString(
                                                    ColorsUtils.colorCcc),
                                                size: Sizes.default14,
                                              ),
                                              alignment: PlaceholderAlignment.middle)
                                        ],
                                      ))),
                                  if (product.tags.isNotEmpty)
                                    Container(
                                      alignment: Alignment.centerLeft,
                                      margin: const EdgeInsets.only(bottom: Sizes.default5),
                                      child: Wrap(
                                        spacing: Sizes.default5,
                                        children: product.tags
                                            .map((e) => Container(
                                                  constraints: const BoxConstraints(
                                                      maxWidth: 45, maxHeight: 15),
                                                  alignment: Alignment.center,
                                                  decoration: BoxDecoration(
                                                      border: Border.all(
                                                          color:
                                                              ColorsUtils.parseColorFromHexString(
                                                                  ColorsUtils.colorCcc)),
                                                      borderRadius:
                                                          BorderRadius.circular(Sizes.default3)),
                                                  child: Text(e,
                                                      style: StylesUtils.customTextStyle(
                                                          fontSize: Sizes.default13,
                                                          color: ColorsUtils.color333)),
                                                ))
                                            .toList(),
                                      ),
                                    ),
                                  if (product.actualPrice != null)
                                    Container(
                                      alignment: Alignment.centerLeft,
                                      margin: const EdgeInsets.only(bottom: Sizes.default5),
                                      child: Wrap(
                                        spacing: Sizes.default10,
                                        children: [
                                          Text(
                                            '₱${product.actualPrice}',
                                            style: StylesUtils.customTextStyle(
                                                fontSize: Sizes.default15,
                                                color: ColorsUtils.colorCc0),
                                          ),
                                          if (product.salePrice != null)
                                            Text(
                                              '${product.salePrice}',
                                              style: TextStyle(
                                                  fontSize: 13,
                                                  decoration: TextDecoration.lineThrough,
                                                  decorationStyle: TextDecorationStyle.solid,
                                                  decorationColor:
                                                      ColorsUtils.parseColorFromHexString(
                                                          ColorsUtils.colorCcc),
                                                  color: ColorsUtils.parseColorFromHexString(
                                                      ColorsUtils.colorCcc),
                                                  fontWeight: FontWeight.normal,
                                                  fontFamily: Fonts.yaHei,
                                                  letterSpacing: 1),
                                            ),
                                        ],
                                      ),
                                    )
                                ],
                              ),
                            ))
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              );
            },
            itemCount: products.length,
            shrinkWrap: true,
          ))
        ],
      ),
    );
  }
}
